package com.dayouzi.crawler_monitor.service.impl;

import com.dayouzi.crawler_monitor.config.DataSource;
import com.dayouzi.crawler_monitor.mapper.CrawlerMapper;
import com.dayouzi.crawler_monitor.pojo.entity.FileJson2;
import com.dayouzi.crawler_monitor.pojo.query.AddDataPolicyQuery;
import com.dayouzi.crawler_monitor.pojo.query.AddDataQuery;
import com.dayouzi.crawler_monitor.pojo.query.CrawlerDataQuery;
import com.dayouzi.crawler_monitor.pojo.query.CrawlerDataUpdateVO;
import com.dayouzi.crawler_monitor.pojo.vo.CrawlerDataVO;
import com.dayouzi.crawler_monitor.pojo.vo.CrawlerPolicyVO;
import com.dayouzi.crawler_monitor.service.CrawlerPolicyService;
import com.dayouzi.crawler_monitor.utils.DateUtils;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
@Slf4j
@DataSource(value = "onlyforcrawlerweb")
public class CrawlerPolicyServiceImpl implements CrawlerPolicyService {

    @Autowired
    private CrawlerMapper crawlerMapper;
    
    private Gson gson = new Gson();

    @Override
    public CrawlerPolicyVO getCrawlerDataById(CrawlerDataQuery crawlerDataQuery) {
        CrawlerPolicyVO crawlerData = crawlerMapper.getCrawlerDataById(crawlerDataQuery);
        // 如果没查到数据
        if (crawlerData == null) {
            return new CrawlerPolicyVO();
        }
        // 处理tag字段为list格式
        String[] splits = crawlerData.getTag().split("、");
        List<String> tags = new ArrayList<>(Arrays.asList(splits));
        crawlerData.setTags(tags);
        
        // 处理intro字段为list
        List<FileJson2> fileList = new ArrayList<>();
        String intro = crawlerData.getIntro();
        if (intro != null && !intro.equals("")) {
            FileJson2[] fileUrls = gson.fromJson(intro, FileJson2[].class);
            for (FileJson2 fileJson2 : fileUrls) {
                String url = fileJson2.getFileUrl();
                String suffix = url.substring(url.lastIndexOf(".") + 1);

                // 正则获取文件上传时间
                int year = 0;
                int month = 0;
                int day = 0;
                int hour = 0;
                int minute = 0;
                int second = 0;
                Pattern pattern = Pattern.compile("default/(\\d{4})(\\d{2})(\\d{2})/(\\d{1,2})/(\\d{1,2})/(\\d{1,2})/");
                Matcher matcher = pattern.matcher(url);
                while (matcher.find()) {
                    year = Integer.parseInt(matcher.group(1));
                    month = Integer.parseInt(matcher.group(2));
                    day = Integer.parseInt(matcher.group(3));
                    hour = Integer.parseInt(matcher.group(4));
                    minute = Integer.parseInt(matcher.group(5));
                    second = Integer.parseInt(matcher.group(6));
                }
                LocalDateTime localDateTime = LocalDateTime.of(year, month, day, hour, minute, second);
                String fileTime = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                
                fileJson2.setFileType(suffix);
                fileJson2.setFileTime(fileTime);
            }
            fileList = Arrays.asList(fileUrls);
        }
        crawlerData.setFileList(fileList);

        // 处理时间戳格式为String格式
        String addTime = crawlerData.getAddTime();
        crawlerData.setAddTime(addTime);
        return crawlerData;
    }

    @Override
    public int updateCommit(CrawlerDataUpdateVO crawlerDataUpdateVO) {
        return crawlerMapper.updateCommit(crawlerDataUpdateVO);
    }

    @Override
    @Transactional
    public int addPolicyData(AddDataPolicyQuery addDataQuery) {
        int id = crawlerMapper.addPolicyData(addDataQuery);
        System.out.println(addDataQuery.getId());
        int result2 = crawlerMapper.addPolicyInfoData(addDataQuery.getId(), addDataQuery.getInfo());
        return id;
    }

    @Override
    public int updateInfoCommit(CrawlerDataUpdateVO crawlerDataUpdateVO) {
        return crawlerMapper.updateInfoCommit(crawlerDataUpdateVO);
    }
}
